#!/usr/bin/expect
set timeout 1000
set host [lindex $argv 0]
set username [lindex $argv 1]
set password [lindex $argv 2]
set src_file [lindex $argv 3]
set dest_file [lindex $argv 4]
set i [lindex $argv 5]

spawn ssh root@$host "mkdir -p /root/shell"
 expect {
 "(yes/no)?"
  {
  send "yes\n"
  expect "*assword:" { send "$password\n"}
 }
 "*assword:"
{
 send "$password\n"
}
}
expect eof

wait

spawn scp $src_file $username@$host:$dest_file
 expect {
 "(yes/no)?"
  {
  send "yes\n"
  expect "*assword:" { send "$password\n"}
 }
 "*assword:"
{
 send "$password\n"
}
}
expect eof

wait

spawn scp redis-6.2.5.tar.gz $username@$host:$dest_file
 expect {
 "(yes/no)?"
  {
  send "yes\n"
  expect "*assword:" { send "$password\n"}
 }
 "*assword:"
{
 send "$password\n"
}
}
expect eof

wait

spawn ssh root@$host "tar -zxvf /root/shell/redis-6.2.5.tar.gz -C /usr/local/ && cd /usr/local/redis-6.2.5/ && make && mkdir -p /usr/local/redis-6.2.5/cluster/conf && mkdir -p /usr/local/redis-6.2.5/cluster/data && mkdir -p /usr/local/redis-6.2.5/cluster/log && mv /root/shell/redis-6371.conf /usr/local/redis-6.2.5/cluster/conf/redis-6371.conf && sed -i 's/ca-ip/$host/g' /usr/local/redis-6.2.5/cluster/conf/redis-6371.conf && sed -i 's/123456/$password/g' /usr/local/redis-6.2.5/cluster/conf/redis-6371.conf && sed -i 's/6371/637$i/g' /usr/local/redis-6.2.5/cluster/conf/redis-6371.conf && mv /usr/local/redis-6.2.5/cluster/conf/redis-6371.conf /usr/local/redis-6.2.5/cluster/conf/redis-637$i.conf && /usr/local/redis-6.2.5/src/redis-server /usr/local/redis-6.2.5/cluster/conf/redis-637$i.conf "
#&& mkdir -p /usr/local/redis-6.2.5/cluster/conf && mkdir -p /usr/local/redis-6.2.5/cluster/data && mkdir -p /usr/local/redis-6.2.5/cluster/log && cd /usr/local/redis-6.2.5 && make"
#&& mv /root/shell/redis-6371.conf /usr/local/redis-6.2.5/cluster/conf/redis-6371.conf && sed -i 's/ca-ip/$host/g' /usr/local/redis-6.2.5/cluster/conf/redis-6371.conf && sed -i 's/123456/$password/g' /usr/local/redis-6.2.5/cluster/conf/redis-6371.conf && sed -i 's/6371/637$i/g' /usr/local/redis-6.2.5/cluster/conf/redis-6371.conf && mv /usr/local/redis-6.2.5/cluster/conf/redis-6371.conf /usr/local/redis-6.2.5/cluster/conf/redis-637$i.conf && /usr/local/redis-6.2.5/src/redis-server /usr/local/redis-6.2.5/cluster/conf/redis-637$i.conf
expect {
		"(yes/no)?"
		{
			send "yes\n"
			expect "*assword:" { send "$password\n"}
		}
		"*assword:"
	{
			send "$password\n"
	}
}	
expect eof
wait